package com.test.nnooolwdeaer.activity_fragment_wooden.login_wooden;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.test.nnooolwdeaer.bean_wooden.StaticVariate_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.GlobalConfig_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.LoginInfo_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.helper_wooden.UserInfoHelper_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.view_wooden.TrackEditText;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.TitleView_wooden;
import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseLoginActivity_wooden;

import org.json.JSONObject;

public class LoginVerCodeActivity_wooden extends BaseLoginActivity_wooden {

    private TrackEditText verCodeEt_wooden;
    private TextView countDownTv_wooden;
    private ViewGroup resendLayout_wooden;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_ver_code_wooden);
        init_wooden();
    }

    @SuppressLint("SetTextI18n")
    private void init_wooden(){
        TitleView_wooden titleView = findViewById(R.id.activity_login_ver_coder_view_title);
        setBackBtn_wooden(titleView.getBackIb_wooden());

        verCodeEt_wooden = findViewById(R.id.activity_login_ver_coder_et_ver_code);
        countDownTv_wooden = findViewById(R.id.activity_login_ver_coder_tv_count_down);
        resendLayout_wooden = findViewById(R.id.activity_login_ver_coder_layout_resend);

        verCodeEt_wooden.sTrackWord(getClass().getSimpleName().toUpperCase()+"_OTP_INPUT");

        TextView mobileTv = findViewById(R.id.activity_login_ver_coder_et_tv_mobile);
        mobileTv.setText(getString(R.string.activity_login_enter_ver_code)+" "+
                StaticVariate_wooden.Country_Mobile_Prefix_Map_wooden.get(StaticVariate_wooden.Select_Country_wooden)+" "+
                UserInfoHelper_wooden.getInstance_wooden().getHideMobile_wooden());

        if (getResources().getConfiguration().locale.getCountry().equalsIgnoreCase("vi")) mobileTv.append(" "+"của bạn");

        findViewById(R.id.activity_login_ver_coder_btn_next).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPointAndUpload(getClass().getSimpleName().toUpperCase()+"_NEXT_CLICK");
            if (TextUtils.isEmpty(verCodeEt_wooden.gText())){
                showToast_wooden(getString(R.string.word_input_tip));
                return;
            }
            startLogin_wooden();
        });

        verCodeEt_wooden.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()==4) startLogin_wooden();
            }
        });

        onRequestOtpCodeResponse_wooden(null);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onRequestOtpCodeResponse_wooden(JSONObject jsonObject) {
        showToast_wooden(getString(R.string.activity_login_ver_code_send_tip));
        resendLayout_wooden.setVisibility(View.GONE);
        resendLayout_wooden.setOnClickListener(null);
        //倒计时
        ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
            int countDown = 60;
            while (countDown-- > 0) {
                try {
                    final int finalCountDown = countDown;
                    ThreadUtil_wooden.getInstance_wooden().runOnUiThread_wooden(() -> {
                        countDownTv_wooden.setText(finalCountDown +"s");
                        if (finalCountDown == 0) {
                            resendLayout_wooden.setVisibility(View.VISIBLE);
                            resendLayout_wooden.setOnClickListener(view -> HttpManager_wooden.getInstance_wooden().requestOtpCode_wooden(UserInfoHelper_wooden.getInstance_wooden().getMobile_wooden()));
                        }
                    });
                    Thread.sleep(999);
                } catch (InterruptedException e) {
                    if (Constant_wooden.Debug) e.printStackTrace();
                }
            }
        });
    }

    //开始登录
    private void startLogin_wooden() {
        String phoneNum = UserInfoHelper_wooden.getInstance_wooden().getMobile_wooden();
        String verCode = verCodeEt_wooden.gText();
        if (TextUtils.isEmpty(phoneNum) || TextUtils.isEmpty(verCode) || verCode.length() < 4) {
            showToast_wooden(getString(R.string.word_input_tip));
            return;
        }

        //测试账号登录
        String accountToken = GlobalConfig_wooden.getInstance_wooden().isTestAccount_wooden(phoneNum);
        if (!TextUtils.isEmpty(accountToken)) {
            try {
                JSONObject object = new JSONObject();
                object.put("token", accountToken);
                object.put("userId", phoneNum);
                object.put("typ", UserInfoHelper_wooden.LOGIN_TYPE_OLD_wooden);
                LoginInfo_wooden.parse_wooden(object);
                onUserLoginResponse_wooden();
            } catch (Exception e) {
                if (Constant_wooden.Debug) e.printStackTrace();
            }
            return;
        }

        HttpManager_wooden.getInstance_wooden().userLogin_wooden(phoneNum, verCode);

    }
}